<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--名称-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            placeholder="请输入名称"
            clearable
            v-model="queryname"
            @input="btnquery()"
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="consulServices"
            :expand-on-click-node="false"
            ref="tree"
            show-checkbox
            @node-click="handleNodeClick"
            @check-change="handleCheckChange"
          />
        </div>
      </el-col>
      <!--单位示例-->
      <el-col :span="20" :xs="24">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              v-permission="['add', $route]"
              type="primary"
              @click="addcompanyshow()"
              plain
              icon="el-icon-plus"
              size="mini"
              >新增单位</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              v-permission="['edit', $route]"
              @click="editcompanyshow()"
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              >编辑单位</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              v-permission="['del', $route]"
              @click="delcompany()"
              >删除单位</el-button
            >
          </el-col>
        </el-row>

        <el-table
          :data="DataList"
          tooltip-effect="dark"
          :header-cell-style="{ background: '#f8f8f9', color: '#515a6e' }"
          style="width: 100%; margin-top: 10px"
        >
          <el-table-column prop="DepartmentName" label="部门名称" width="300">
          </el-table-column>
          <el-table-column>
            <template slot="header">
              操作
              <el-button
                v-permission="['add', $route]"
                type="primary"
                size="small"
                @click="editdepartmentshow()"
                >新增部门</el-button
              >
            </template>
            <template slot-scope="scope">
              <el-button
                v-permission="['edit', $route]"
                type="primary"
                size="small"
                @click="editdepartmentshow(scope.row)"
                >编辑信息</el-button
              >
              <el-button
                v-permission="['del', $route]"
                type="danger"
                size="small"
                @click="deldepartment(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <Pagination
          v-bind:child-msg="pageparm"
          @callFather="callFather"
        ></Pagination>
      </el-col>
    </el-row>

    <!-- 编辑界面 -->
    <el-dialog
      :title="title"
      :visible.sync="editFormVisible"
      width="40%"
      :close-on-click-modal="false"
      @click="closeDialog('company')"
    >
      <el-form label-width="80px" ref="editForm" :model="editForm">
        <el-form-item label="企业名称" prop="CompanyName">
          <el-input
            size="small"
            v-model="editForm.CompanyName"
            placeholder="企业名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog('company')">取消</el-button>
        <el-button
          size="small"
          type="primary"
          class="title"
          @click="addcompany()"
          >保存</el-button
        >
      </div>
    </el-dialog>

    <!-- 编辑部门界面 -->
    <el-dialog
      :title="title"
      :visible.sync="editformDepartment"
      width="40%"
      :close-on-click-modal="false"
      @click="closeDialog('department')"
    >
      <el-form
        label-width="80px"
        ref="departmentmodel"
        :model="departmentmodel"
      >
        <el-form-item label="所在单位" prop="CompanyName">
          <el-input
            size="small"
            :value="multipleSelection.label"
            placeholder="所在单位"
            disabled
          ></el-input>
        </el-form-item>

        <el-form-item label="部门名称" prop="DepartmentName">
          <el-input
            size="small"
            v-model="departmentmodel.DepartmentName"
            placeholder="部门名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog('department')"
          >取消</el-button
        >
        <el-button
          size="small"
          type="primary"
          class="title"
          @click="adddepartment()"
          >保存</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { req, reqjson } from "@/api/axiosFun";

import Pagination from "../../components/Pagination";
export default {
  name: "ConsulManager",

  data() {
    return {
      loading: false, //是显示加载
      title: "",
      editFormVisible: false,
      editformDepartment: false,
      editForm: {
        CompanyName: "",
        GuId: "",
      },
      departmentmodel: {
        DepartmentName: "",
        GuId: "",
      },
      consulServices: [],
      companylist: [], //定义单位数据
      DataList: [],
      multipleSelection: { label: "", id: "" },
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10,
      },

      queryname: "", //查询单位名称
    };
  },
  // 注册组件
  components: {
    Pagination,
  },
  created() {
    this.getcompanydata();
  },
  methods: {
    getcompanydata() {
      this.loading = true;
      reqjson(
        "post",
        "/SysCompany/GetCompanyList?token=" + this.$jsutils.sessionget("token")
      )
        .then((x) => {
          this.loading = false;
          if (x.success) {
            this.consulServices = x.data;
            this.companylist = x.data;
            if (x.data.length > 0) {
              this.multipleSelection = x.data[0];
              this.getDepartmentdata(this.multipleSelection.id);
            }
          }
        })
        .catch((x) => {
          this.loading = false;
          console.log(x);
        });
    },
    // 获取数据方法
    getDepartmentdata(companyguid) {
      let data = {
        page: this.pageparm.currentPage,
        limit: this.pageparm.pageSize,
        data: companyguid,
      };
      this.loading = true;
      reqjson(
        "post",
        "/SysCompany/GetDepartment?token=" + this.$jsutils.sessionget("token"),
        data
      )
        .then((x) => {
          this.loading = false;
          if (x.success) {
            this.pageparm.currentPage = x.data.CurrentPage;
            this.pageparm.pageSize = x.data.ItemsPerPage;
            this.pageparm.total = x.data.TotalItems;
            this.DataList = x.data.Items;
          }
        })
        .catch((x) => {
          this.loading = false;
          console.log(x);
        });
    },
    // 分页插件事件
    callFather(parm) {
      this.pageparm.currentPage = parm.currentPage;
      this.pageparm.pageSize = parm.pageSize;
      this.getDepartmentdata(this.multipleSelection.id);
    },
    //新增单位信息
    addcompanyshow() {
      this.editFormVisible = true;
      this.title = "新增单位";
      this.editForm = {
        CompanyName: "",
        GuId: "",
      };
    },

    addcompany() {
      if (this.$jsutils.isempty(this.editForm.CompanyName)) {
        this.$message.error("单位名称不能为空！");
        return false;
      }
      this.loading = true;
      reqjson(
        "post",
        "/SysCompany/AddCompany?token=" + this.$jsutils.sessionget("token"),
        this.editForm
      )
        .then((x) => {
          this.loading = false;
          if (x.success) {
            this.editFormVisible = false;
            this.getcompanydata();
            this.$message.success("操作成功!");
          } else {
            this.$message.error(x.data);
          }
        })
        .catch((x) => {
          this.loading = false;
          console.log(x);
        });
    },
    //编辑单位信息
    editcompanyshow() {
      if (this.$jsutils.isempty(this.editForm.GuId)) {
        this.$message.error("请选择单位信息！");
        return false;
      }
      this.editFormVisible = true;
      this.title = "编辑单位信息";
    },
    //移除单位信息
    delcompany() {
      if (this.$jsutils.isempty(this.editForm.GuId)) {
        this.$message.error("单位不能为空！");
        return false;
      }
      this.$confirm(`确定要删除【${this.editForm.CompanyName}】吗?`, "信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.loading = true;
          reqjson(
            "post",
            "/SysCompany/DelCompany?token=" +
              this.$jsutils.sessionget("token") +
              "&guid=" +
              this.editForm.GuId
          )
            .then((x) => {
              this.loading = false;
              if (x.success) {
                this.getcompanydata();
                this.$message.success("操作成功!");
              }
            })
            .catch((x) => {
              this.loading = false;
              console.log(x);
            });
        })
        .catch((x) => {
          this.$message({
            type: "info",
            message: "已取消删除！",
          });
        });
    },
    //编辑部门信息
    editdepartmentshow(row) {
      if (!!row) {
        if (this.$jsutils.isempty(row.GuId)) {
          this.$message.error("请选择单位信息！");
          return false;
        }
        this.departmentmodel = {
          DepartmentName: row.DepartmentName,
          GuId: row.GuId,
        };
        this.editformDepartment = true;
        this.title = "编辑部门";
      } else {
        this.departmentmodel = {
          DepartmentName: "",
          GuId: "",
        };
        this.editformDepartment = true;
        this.title = "新增部门";
      }
    },
    //新增部门数据
    adddepartment() {
      if (this.$jsutils.isempty(this.departmentmodel.DepartmentName)) {
        this.$message.error("部门不能为空！");
        return false;
      }

      this.loading = true;
      this.departmentmodel.CompanyGuId = this.multipleSelection.id;
      reqjson(
        "post",
        "/SysCompany/AddDepartment?token=" + this.$jsutils.sessionget("token"),
        this.departmentmodel
      )
        .then((x) => {
          this.loading = false;
          if (x.success) {
            this.editformDepartment = false;
            this.getDepartmentdata(this.multipleSelection.id);
            this.$message.success("操作成功!");
          }
        })
        .catch((x) => {
          this.loading = false;
          console.log(x);
        });
    },
    //查询单位列表
    btnquery() {
      if (!!this.queryname) {
        this.consulServices = this.companylist.filter((e) => {
          return e.label.indexOf(this.queryname) > -1;
        });
         if ( this.consulServices.length > 0) {
              this.multipleSelection =  this.consulServices[0];
              this.getDepartmentdata(this.multipleSelection.id);
            }
      } else {
        this.consulServices = this.companylist;
        if ( this.consulServices.length > 0) {
              this.multipleSelection =  this.consulServices[0];
              this.getDepartmentdata(this.multipleSelection.id);
            }
      }
    },
    //移除部门数据
    deldepartment(row) {
      if (this.$jsutils.isempty(row.GuId)) {
        this.$message.error("部门姓名不能为空！");
        return false;
      }
      this.$confirm("确定要删除部门数据吗?", "信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.loading = true;
          reqjson(
            "post",
            "/SysCompany/DelDepartment?token=" +
              this.$jsutils.sessionget("token") +
              "&guid=" +
              row.GuId
          )
            .then((x) => {
              this.loading = false;
              if (x.success) {
                this.editFormVisible = false;
                this.getDepartmentdata(this.multipleSelection.id);
                this.$message.success("操作成功!");
              }
            })
            .catch((x) => {
              this.loading = false;
              console.log(x);
            });
        })
        .catch((x) => {
          this.$message({
            type: "info",
            message: "已取消删除！",
          });
        });
    },
    // 关闭编辑、增加弹出框
    closeDialog(dialog) {
      if (dialog == "company") {
        this.editFormVisible = false;
        this.editForm = {
          CompanyName: "",
          GuId: "",
        };
      } else {
        this.editformDepartment = false;
        this.departmentmodel = {
          DepartmentName: "",
          GuId: "",
        };
      }
    },
    //多选列表事件
    handleCheckChange(data, checked, indeterminate) {
      this.editForm = {
        CompanyName: data.label,
        GuId: data.id,
      };
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.multipleSelection = data;

      this.getDepartmentdata(data.id);
    },
  },
};
</script>
